﻿<Window x:Class="CardPlay.Client.Views.GameBrowserView" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:CardPlay.Client.Controls" 
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
        xmlns:Converters="clr-namespace:CardPlay.Client.Converters" MinHeight="500" MinWidth="800" WindowStartupLocation="CenterOwner" Width="800" Height="600">
    <Window.Resources>
        <Converters:EnumToDescriptionNameConvereter x:Key="EnumConverter" />
        <Converters:EnumToValueConverter x:Key="EnumToIntConverter" />

        <Style x:Key="BlockStyle" TargetType="local:BlockGauge">
            <Setter Property="HorizontalAlignment" Value="Left" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Margin" Value="4" />
            <Setter Property="Width" Value="60" />
            <Setter Property="Height" Value="8" />
            <Setter Property="StartColor" Value="YellowGreen" />
            <Setter Property="EndColor" Value="DarkGreen" />
            <Setter Property="Minimum" Value="-1" />
            <Setter Property="Maximum" Value="5" />
        </Style>

        <DataTemplate x:Key="LevelOfSkillTemplate">
            <local:BlockGauge Style="{StaticResource BlockStyle}" Value="{Binding LevelOfSkill, Mode =OneWay, Converter={StaticResource EnumToIntConverter}}" >
                <local:BlockGauge.ToolTip>
                    <TextBlock Text="{Binding LevelOfSkill, Converter={StaticResource EnumConverter}}" />
                </local:BlockGauge.ToolTip>
            </local:BlockGauge>
        </DataTemplate>

        <DataTemplate x:Key="LevelOfDifficultyTemplate">
            <local:BlockGauge Style="{StaticResource BlockStyle}" Value="{Binding LevelOfDifficulty, Mode=OneWay, Converter={StaticResource EnumToIntConverter}}" >
                <local:BlockGauge.ToolTip>
                    <TextBlock Text="{Binding LevelOfDifficulty, Converter={StaticResource EnumConverter}}" />
                </local:BlockGauge.ToolTip>
            </local:BlockGauge>
        </DataTemplate>

        <DataTemplate x:Key="PlayingTimeTemplate">
            <local:BlockGauge Style="{StaticResource BlockStyle}" Value="{Binding TypicalPlayingTime, Mode=OneWay, Converter={StaticResource EnumToIntConverter}}" >
                <local:BlockGauge.ToolTip>
                    <TextBlock Text="{Binding TypicalPlayingTime, Converter={StaticResource EnumConverter}}" />
                </local:BlockGauge.ToolTip>
            </local:BlockGauge>
        </DataTemplate>

        <Style TargetType="local:SearchTextBox">
            <Setter Property="Width" Value="140" />
            <Setter Property="HorizontalAlignment" Value="Right" />
        </Style>

        <Style TargetType="{x:Type ComboBoxItem}" >
            <Style.Triggers>
                <DataTrigger Binding="{Binding}" Value="{x:Null}" >
                    <Setter Property="IsEnabled" Value="false" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ComboBoxItem">
                                <Separator HorizontalAlignment="Stretch" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>
    <DockPanel Margin="4">
        <!-- Buttons -->
        <DockPanel DockPanel.Dock="Bottom" Margin="0,4,0,0">
            <DockPanel.Resources>
                <Style TargetType="Button">
                    <Setter Property="MinWidth" Value="85" />
                </Style>
            </DockPanel.Resources>
            <StackPanel DockPanel.Dock="Right" Orientation="Horizontal">
                <Button x:Name="OpenItem" Content="_Open" IsDefault="True" Margin="0,0,6,0" />
                <Button Content="Cancel" IsCancel="True" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock VerticalAlignment="Center" Margin="2,0,6,0" >
                    Total Games: <Run Text="{Binding TotalGames, Mode=OneTime}" />
                </TextBlock>
            </StackPanel>
        </DockPanel>
        
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition MinHeight="100" />
                <RowDefinition Height="Auto" />
                <RowDefinition MinHeight="200"/>
            </Grid.RowDefinitions>

            <!-- Top panel -->
            <DockPanel>
                <DockPanel.Resources>
                    <ResourceDictionary>
                        <ResourceDictionary.MergedDictionaries>
                            <ResourceDictionary Source="/Themes/DataGrid.xaml" />
                        </ResourceDictionary.MergedDictionaries>
                    </ResourceDictionary>
                </DockPanel.Resources>

                <Grid DockPanel.Dock="Top" Margin="0,0,0,4">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="220"/>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <!-- View tools -->
                    <DockPanel>
                        <Label Content="_Group by:" VerticalAlignment="Center" Margin="1,-2,6,-2"/>
                        <ComboBox SelectedItem="{Binding SelectedGrouping}" ItemsSource="{Binding GroupingActions}" />
                    </DockPanel>
                    <local:SearchTextBox LabelText="Search" Grid.Column="2" Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" />
                </Grid>
                
                <DataGrid x:Name="GameTable" ItemsSource="{Binding GameCollection}" SelectedItem="{Binding SelectedGame}" 
                          FrozenColumnCount="1" IsReadOnly="true" Style="{StaticResource DataGridStyle}">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDoubleClick">
                            <i:InvokeCommandAction Command="{Binding ActivateItem}" 
                                                   CommandParameter="{Binding ElementName=GameTable, Path=SelectedItem, Mode=OneWay}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>

                    <DataGrid.GroupStyle>
                        <GroupStyle ContainerStyle="{StaticResource GroupItemStyle}"/>
                    </DataGrid.GroupStyle>

                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Name" MinWidth="120" CanUserReorder="False" Binding="{Binding DisplayName}" />
                        <DataGridTextColumn Header="Decks" MinWidth="60" Binding="{Binding NumberOfDecks}" HeaderStyle="{StaticResource CenterAlignHeaderStyle}" CellStyle="{StaticResource CenterAlignCellStyle}" />
                        <!--<DataGridTextColumn Header="Difficulty Level" Binding="{Binding LevelOfDifficulty, Converter={StaticResource EnumConverter}}" />-->
                        <DataGridTemplateColumn Header="Difficulty" SortMemberPath="LevelOfDifficulty" CellTemplate="{StaticResource LevelOfDifficultyTemplate}" />
                        <DataGridTemplateColumn Header="Game Time" SortMemberPath="TypicalPlayingTime" CellTemplate="{StaticResource PlayingTimeTemplate}" />
                        <!--<DataGridTextColumn Header="Game Time" MinWidth="100" Binding="{Binding TypicalPlayingTime, Converter={StaticResource EnumConverter}}" />-->
                        <DataGridTemplateColumn Header="Skill Level" SortMemberPath="LevelOfSkill" CellTemplate="{StaticResource LevelOfSkillTemplate}" />
                        <DataGridTextColumn Header="Family" MinWidth="120" Binding="{Binding Family}"/>
                        <DataGridTextColumn Header="Last Played"  MinWidth="120"/>
                        <DataGridTextColumn Header="Last Outcome" />
                    </DataGrid.Columns>
                </DataGrid>
            </DockPanel>

            <GridSplitter Grid.Row="1" ResizeDirection="Rows" Height="5" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ResizeBehavior="PreviousAndNext" />

            <!-- Bottom panel -->
            <DockPanel Grid.Row="2" >
                <Grid DockPanel.Dock="Top" ClipToBounds="true">
                    <TextBlock Text="{Binding Path=SelectedGame.DisplayName}" FontSize="46" FontWeight="Bold" Margin="4,-8,4,-14" Foreground="Gainsboro"/>
                    <local:SearchTextBox LabelText="Game Number" Text="{Binding GameNumber}" Icon="pack://application:,,,/CardPlay.Client;component/Assets/refresh.png"
                        VerticalAlignment="Bottom" Margin="0,0,0,4" SearchMode="Delayed" >
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Search">
                                <i:InvokeCommandAction Command="{Binding RandomGameNumber}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </local:SearchTextBox>
                </Grid>
                <TabControl TabStripPlacement="Bottom">
                    <TabItem Header="Overview">
                        <ContentControl x:Name="Preview" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" />
                    </TabItem>

                    <TabItem Header="Rules">
                        <FlowDocumentReader>
                            <FlowDocument FontFamily="Segoe UI" FontSize="12">
                                <Paragraph>
                                    TODO: Display rules
                                </Paragraph>
                            </FlowDocument>
                        </FlowDocumentReader>
                    </TabItem>

                    <TabItem Header="History">
                        <Grid>
                            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="No game selected" />
                            <ContentControl x:Name="History" />
                        </Grid>
                    </TabItem>
                </TabControl>
            </DockPanel>
        </Grid>
    </DockPanel>
</Window>
